<?php 
/**
 * @author Eugenio Fage
 * http://eugeniofage.wordpress.com/
 * http://code.google.com/p/monky/
 * 
 */
 
$this->Html->script('theme/cats',false);

$CATS=ClassRegistry::init('Category')->getCats();

$selected_cat=(isset($this->params['pass'][0]) && $this->params['controller']=='links' && $this->params['action']!='view')? $this->params['pass'][0] : 0;

?>
	<h3><?php __('Categories'); ?></h3>
   	<ul id="catsli">
<?
foreach ($CATS as $_cat){
	$_expand=false;
	$_subcats='';

	$controller='links';
	$action='index';
	if(in_array($this->params['action'],array('index','shakeit','popular'))){
		$action=$this->params['action'];
	}
	
	$_url=compact('controller','action');
	
	foreach ($_cat['children'] as $_child){
		$_child=$_child['Category'];
		$_ops=array();
		if($_child['id']==$selected_cat){
			$_expand=true;
			$_ops+=array('style'=>'background:none repeat scroll 0 0 #F0F09A');
		}
		
		$_subcats.=$this->Html->tag('li', $this->Html->link($_child['name'],$_url+array('slug'=>Inflector::slug($_child['name']),$_child['id']),$_ops));
	}

	if($_cat['Category']['id']==$selected_cat){
		$_expand=true;
	}
	?>
	<li>
	  <div<?=($_expand)? ' style="background:none repeat scroll 0 0 #F0F09A"':'' ?>>
	    <a class="expand-cat" style="cursor:pointer"><?=($_expand)? '-' : '+' ?></a>
		<?=$this->Html->link($_cat['Category']['name'],$_url+array('slug'=>Inflector::slug($_cat['Category']['name']),$_cat['Category']['id'])) ?>
	  </div>
		<?
		$_ops=array('class'=>'subcatsli');
		if($_expand) $_ops+=array('style'=>'display:block');
		
		echo $this->Html->tag('ul',$_subcats,$_ops);
		?>
	</li>		
<?php 
}
?>
	</ul>